#include <stdio.h>
#include <stdlib.h>
#include "master.h"
#include "coid/comm/binstream/stdstream.h"
#include "coid/comm/binstream/binstream.h"

CMaster::CMaster(void)
{
// Moved to accept connect
}

CMaster::~CMaster(void)
{
// voif
}

// In coidized version here we use accept_connect for this functionality from constructor
net_fnc_s opcd CMaster::accept_connect_shared()
{
   return 0; //
}

net_fnc_s opcd CMaster::accept_connect()
{
   cmdin->set_option( cmd_interface::OPTION_ALLOW_SHARED_ACCESS, 1 );
   cmdin->set_option( cmd_interface::OPTION_LOG_CALLS, 1 );
   return 0; // FIXME return value
}

net_fnc_s opcd CMaster::accept_startup_params( cmd_interface& cmd )
{
   cmdin = &cmd;
   cmd.get_server_address(srvaddr);
   return 0;
}

net_fnc net_nolock opcd CMaster::insertMasterMessage(net_in int value)
{
   printf("Master message inserting: %d\n", value);
   coid::stdoutstream __s; 
   opcd e;
   if (value == 1)
   {    
      sLeft = new CSlave_client(); 
      printf("going to connect to CSlave_client left\n");
      // connect to coid server and submit '3' as constructor (accept_connect) parameter  
      e = sLeft->connect( master_address_2, cmdin->get_id(), slave_address_2);
      //   opcd e = sLeft->connect( srvaddr, cmdin->get_id(), "localhost:4444");
      __s << "e " << opcd_formatter(e) << BINSTREAM_FLUSH;
      printf("going to connect to CSlave_client right\n");
      sRight = new CSlave_client(); 
      //   e = sRight->connect( srvaddr, cmdin->get_id(), "localhost:4444");
      e = sRight->connect( master_address_1, cmdin->get_id(), slave_address_1);
      __s << "e " << opcd_formatter(e) << BINSTREAM_FLUSH;
   }
   else if (value == 2)
   {
      e = sRight->insertSlaveMessage(5);
      __s << "e " << opcd_formatter(e) << BINSTREAM_FLUSH;
      e = sLeft->insertSlaveMessage(6);
      __s << "e " << opcd_formatter(e) << BINSTREAM_FLUSH;
   }
}
